//
//  SplashViewController.m
//  OCFrame
//
//  Created by hfzhangzhang on 2017/2/22.
//  Copyright © 2017年 hfzhangzhang. All rights reserved.
//

#import "SplashViewController.h"
#import "LoginViewController.h"
#import "RegisterViewController.h"

static NSString *Show_SplashView_Before = @"Splash_View_Shown_Before";

@interface SplashViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong)  UIScrollView* scrollView;


@end

@implementation SplashViewController

+ (void)showSplashIfNeeded
{
  if ([[NSUserDefaults standardUserDefaults] boolForKey:Show_SplashView_Before]) return;
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:[[self alloc] init] animated:NO completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    NSArray *imageNames = @[
                            @"image_splash_01",
                            @"image_splash_02",
                            @"image_splash_03"
                            ];
    UIView *lastView = nil;
    for (NSString *imageName in imageNames) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [self.scrollView addSubview:imageView];
        
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            if (lastView) {
                make.left.equalTo(lastView.mas_right);
            } else {
                make.left.equalTo(self.scrollView);
            }
            make.top.bottom.equalTo(self.view);
            make.width.equalTo(self.view);
        }];
        
        lastView = imageView;
    }

    [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.scrollView);
    }];
    
    lastView.userInteractionEnabled = YES;

    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    loginButton.layer.cornerRadius = 7.5;
    loginButton.layer.masksToBounds = YES;
    loginButton.layer.borderColor =  kColorFromRGB(0xF46B7B).CGColor;
    loginButton.layer.borderWidth = .5;
    loginButton.titleLabel.font = [UIFont systemFontOfSize:16];
    //[loginButton zz_setBackgroundColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton setTitleColor:kColorFromRGB(0xF46B7B) forState:UIControlStateNormal];
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(loginButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [lastView addSubview:loginButton];
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(lastView.mas_centerX);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(100);
        make.top.mas_equalTo(lastView.mas_bottom).offset(-60.0);
    }];

    
    [[loginButton rac_signalForControlEvents:UIControlEventTouchUpOutside] subscribeNext:^(id x) {
        NSLog(@"1");
    }];
}


-(UIScrollView *)scrollView
{
    if (_scrollView==nil) {
        _scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,kScreen_Width,kScreen_Height)];
        _scrollView.backgroundColor = [UIColor clearColor];
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        _scrollView.bounces = NO;//防止侧滑反弹
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}

- (void)loginButtonClicked {
    [self dismiss];
}

- (void)dismiss {
    
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:Show_SplashView_Before];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [self dismissViewControllerAnimated:NO completion:^{
        
    }];
}

@end
